package com.sixth.oa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sixth.oa.entity.model.sixthResource;
import com.sixth.oa.mapper.sixthResourceMapper;
import com.sixth.oa.util.ResultStr;

@RequestMapping("/resource")
@Controller
public class ResourceController {
	
	@Autowired
	sixthResourceMapper resourceMapper;
	
	
	/**
	 * 
	 * @param resource
	 * @return
	 */
//	@PostMapping("/addResource")
//	@ResponseBody
//	public ResultStr addResource(sixthResource resource) {
//		int i = resourceMapper.insert(resource);
//		if(i>0) {
//			return new ResultStr(200,"添加成功",null);
//		}else {
//			return new ResultStr(400,"网络错误请重试",null);
//		}
//	}
	
	/**
	 * 添加资源
	 * @param resource
	 * @return
	 */
	@PostMapping("/addResource")
	@ResponseBody
	public String addResource(sixthResource resource) {
		int i = resourceMapper.insert(resource);
		if(i>0) {
			return "添加成功,请刷新页面";
		}else {
			return "你编程好像蔡徐坤";
		}
	}
	
	/**
	 * 查询资源
	 * @return
	 */
	@PostMapping("/getResource")
	@ResponseBody
	public ResultStr getResource() {
		List<sixthResource> list = resourceMapper.selectAll();
		return new ResultStr(0,"sucecc",list);
	}
	
	/**
	 * 修改资源
	 * @param resource
	 * @return
	 */
	@PostMapping("/updateR")
	@ResponseBody
	public String updateResource(sixthResource resource) {
		int i = resourceMapper.updateByPrimaryKey(resource);
		if(i>0) {
			return "修改成功,请刷新页面";
		}else {
			return "你编程好像蔡徐坤";
		}
	}
	
	/**
	 * 查询单个资源
	 * @param id
	 * @return
	 */
	@PostMapping("/selectById")
	@ResponseBody
	public ResultStr selectById(Integer id) {
		sixthResource res = resourceMapper.selectByPrimaryKey(id);
		return new ResultStr(0,"sucecc",res);
	}
	
	/**
	 * 删除资源
	 * @param id
	 * @return
	 */
	@PostMapping("/deleteById")
	@ResponseBody
	public ResultStr deleteResource(Integer id) {
		int i = resourceMapper.deleteByPrimaryKey(id);
		if(i>0) {
			 return new ResultStr(0,"sucecc",null);
		}else {
			 return new ResultStr(400,"你编程好像蔡徐坤",null);
		}
	}
}
